package dao;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.TypedQuery;

import dto.EmployeeDTO;
import entities.Employee;

/**
 * Session Bean implementation class EmployeeDAO
 */
@Stateless
public class EmployeeDAO implements EmployeeDAORemote {

	@PersistenceContext
	EntityManager em;
	
	Conversion conv = new Conversion();

	/**
	 * Default constructor.
	 */
	public EmployeeDAO() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public EmployeeDTO findEmployeeByUsernameAndPassword(EmployeeDTO employeeDTO) {
		Query query = em.createNamedQuery("findEmployeeByUsernameAndPassword");
		query.setParameter("usernameParam", employeeDTO.getUsername());
		query.setParameter("userpassParam", employeeDTO.getPassword());
		List<Employee> employee = query.getResultList();
		EmployeeDTO result = new EmployeeDTO();
		if (employee != null && employee.size() > 0) {
			
			try {
				result.setEmail(employee.get(0).getEmail());
				result.setPassword(employee.get(0).getPassword());
				result.setUsername(employee.get(0).getUsername());
				result.setId(employee.get(0).getId());
				result.setRank(employee.get(0).getRank());
				result.setDateofbirth(employee.get(0).getDateofbirth());
				result.setAreaManager(employee.get(0).getAreaManager());
				result.setContactpersonde(employee.get(0).getContactpersonde());
				result.setDepartment(employee.get(0).getDepartment());
				result.setDeparture_date(employee.get(0).getDeparture_date());
				result.setEmpl_type(employee.get(0).getEmpl_type());
				result.setEmployment_date(employee.get(0).getEmployment_date());
				result.setFirstname(employee.get(0).getFirstname());
				result.setLastmedicalvisit(employee.get(0).getLastmedicalvisit());
				result.setLastname(employee.get(0).getLastname());
				result.setNet_salary(employee.get(0).getNet_salary());
				result.setPosition(employee.get(0).getPosition());
				result.setProfile(employee.get(0).getProfile());
				result.setProsisnr(employee.get(0).getProsisnr());
				result.setTeamleaderro(employee.get(0).getTeamleaderro());
				result.setVar_part(employee.get(0).getVar_part());
				result.setSelected_language(employee.get(0).getSelected_language());
				
			} catch (Exception e1) {
				e1.printStackTrace();
			}
			

		}
		else
			System.out.println("KAKA!!!!");
		return result;
	}

	@Override
	public EmployeeDTO getEmployeeById(int id) {
		Employee e = new Employee();
		e = em.find(Employee.class, id);
		EmployeeDTO ee = new EmployeeDTO();
		ee = conv.convertEmployeeToDTO(e);
		return ee;
	}
	
	public String findAdminEmail(){
		Employee e = new Employee();
		Query query = em.createNamedQuery("findAdministrator");
		query.setParameter("rankParam", 1);
		e = (Employee) query.getSingleResult();
		System.out.println("Admin Email: " + e.getEmail());
		return e.getEmail();
	}

	@Override
	public EmployeeDTO findEmployeeByTeamLeader(EmployeeDTO employeeDTO) {
		
		return null;
	}

	@Override
	public EmployeeDTO findTLIdFromEmployee(EmployeeDTO employeeDTO) {
		String firstn = "";
		String lastn = "";
		String name = "";
		name = employeeDTO.getTeamleaderro();
		int bool = 0;
		char[] arr = name.toCharArray();
		for (int i = 0; i<name.length(); i++){
			if (bool == 0){
				if (arr[i] != ' '){
					firstn = firstn + arr[i];
				}
				else
					bool = 1;
			}
			else
				lastn = lastn + arr[i];
		}
		Query query = em.createNamedQuery("findEmployeeByFirstAndLastName");
		query.setParameter("firstnameParam", firstn);
		query.setParameter("lastnameParam", lastn);
		List<Employee> employee = query.getResultList();
		if (employee != null && employee.size() > 0) {
		}
		EmployeeDTO eDTO = new EmployeeDTO();
		eDTO.setId(employee.get(0).getId());
		
		return eDTO;
	}

	@Override
	public void add(EmployeeDTO eDTO) {
		try{
			Employee e = new Employee();
			e.setLastname(eDTO.getLastname());
			e.setFirstname(eDTO.getFirstname());
			e.setUsername(eDTO.getUsername());
			e.setPassword(eDTO.getPassword());
			e.setLastmedicalvisit(eDTO.getLastmedicalvisit());
			e.setEmail(eDTO.getEmail());
			e.setRank(eDTO.getRank());
			e.setDepartment(eDTO.getDepartment());
			e.setDateofbirth(eDTO.getDateofbirth());
			e.setAreaManager(eDTO.getAreaManager());
			e.setPosition(eDTO.getPosition());
			e.setContactpersonde(eDTO.getContactpersonde());
			e.setTeamleaderro(eDTO.getTeamleaderro());
			e.setEmpl_type(eDTO.getEmpl_type());
			e.setVar_part(eDTO.getVar_part());
			e.setNet_salary(eDTO.getNet_salary());
			e.setDeparture_date(eDTO.getDeparture_date());
			e.setEmployment_date(eDTO.getEmployment_date());
			e.setProsisnr(eDTO.getProsisnr());
			e.setProfile(eDTO.getProfile());
			e.setSelected_language("EN");
			em.persist(e);
		}
		catch (Exception ee){
			ee.printStackTrace();
			
		}
		
	}

	@Override
	public List<EmployeeDTO> findAllAngajati() {
		Query query = em.createNamedQuery("getAllEmployees");
		List<Employee> employee = query.getResultList();
		List<EmployeeDTO> result = new ArrayList<EmployeeDTO>();
		for (int i = 0; i<employee.size(); i++){
			EmployeeDTO e = new EmployeeDTO();
			e.setId(employee.get(i).getId());
			e.setFirstname(employee.get(i).getFirstname());
			e.setLastname(employee.get(i).getLastname());
			e.setAreaManager(employee.get(i).getAreaManager());
			e.setContactpersonde(employee.get(i).getContactpersonde());
			e.setDateofbirth(employee.get(i).getDateofbirth());
			e.setDepartment(employee.get(i).getDepartment());
			e.setDeparture_date(employee.get(i).getDeparture_date());
			e.setEmail(employee.get(i).getEmail());
			e.setEmpl_type(employee.get(i).getEmpl_type());
			e.setEmployment_date(employee.get(i).getEmployment_date());
			e.setLastmedicalvisit(employee.get(i).getLastmedicalvisit());
			e.setNet_salary(employee.get(i).getNet_salary());
			e.setPassword(employee.get(i).getPassword());
			e.setPosition(employee.get(i).getPosition());
			e.setProfile(employee.get(i).getProfile());
			e.setProsisnr(employee.get(i).getProsisnr());
			e.setRank(employee.get(i).getRank());
			e.setTeamleaderro(employee.get(i).getTeamleaderro());
			e.setUsername(employee.get(i).getUsername());
			e.setVar_part(employee.get(i).getVar_part());
			e.setSelected_language(employee.get(i).getSelected_language());
			result.add(e);
		}
		return result;
	}

	@Override
	public void del(EmployeeDTO employee) {
		int id = employee.getId();
		Employee e = new Employee();
		e = em.find(Employee.class, id);
		em.remove(e);
		
	}

	@Override
	public void edit(EmployeeDTO employee) { 
		Employee e = new Employee();
		e = em.find(Employee.class, employee.getId());
		e.setFirstname(employee.getFirstname());	// baj van ebben a sorban :|
		e.setLastname(employee.getLastname());
		e.setAreaManager(employee.getAreaManager());
		e.setContactpersonde(employee.getContactpersonde());
		e.setDateofbirth(employee.getDateofbirth());
		e.setDepartment(employee.getDepartment());
		e.setDeparture_date(employee.getDeparture_date());
		e.setEmail(employee.getEmail());
		e.setEmpl_type(employee.getEmpl_type());
		e.setEmployment_date(employee.getEmployment_date());
		e.setLastmedicalvisit(employee.getLastmedicalvisit());
		e.setNet_salary(employee.getNet_salary());
		e.setPassword(employee.getPassword());
		e.setPosition(employee.getPosition());
		e.setProfile(employee.getProfile());
		e.setProsisnr(employee.getProsisnr());
		e.setRank(employee.getRank());
		e.setTeamleaderro(employee.getTeamleaderro());
		e.setUsername(employee.getUsername());
		e.setVar_part(employee.getVar_part());
		e.setSelected_language(employee.getSelected_language());
	}

	/*
	 * va fi schimbat metoda getAngajatiByOne si getAngajatiByTwo, din cauza asta nu am schimbat query-urile.
	 * 
	 */
	
	
	@Override
	public List<EmployeeDTO> getAngajatiByTwo(String f1, String v1, String f2,
			String v2) {
		Query query = em.createNamedQuery("getAllEmployees");
		List<EmployeeDTO> angajati = new ArrayList<EmployeeDTO>();
		if ((("Nume".equals(f1)) && ("Prenume".equals(f2))) || (("Prenume".equals(f1)) && ("Nume".equals(f2))))
			query = em.createQuery("SELECT e FROM Employee e WHERE e.lastname = :Param1 AND e.firstname = :Param2",
					Employee.class);
		else if ((("Nume".equals(f1)) && ("Departament".equals(f2))) || (("Departament".equals(f1)) && ("Nume".equals(f2))))
			query = em.createQuery("SELECT e FROM Employee e WHERE e.lastname=:Param1 AND e.department=:Param2", 
					Employee.class);
		else if ((("Nume".equals(f1)) && ("Team Leader".equals(f2))) || (("Team Leader".equals(f1)) && ("Nume".equals(f2))))
			query = em.createQuery("SELECT e FROM Employee e WHERE e.lastname=:Param1 AND e.teamleaderro=:Param2", 
					Employee.class);
		else if ((("Nume".equals(f1)) && ("Tipul angajarii".equals(f2))) || (("Tipul angajarii".equals(f1)) && ("Nume".equals(f2))))
			query = em.createQuery("SELECT e FROM Employee e WHERE e.lastname=:Param1 AND e.empl_type=:Param2", 
					Employee.class);
		else if ((("Nume".equals(f1)) && ("Pozitie".equals(f2))) || (("Pozitie".equals(f1)) && ("Nume".equals(f2))))
			query = em.createQuery("SELECT e FROM Employee e WHERE e.lastname=:Param1 AND e.position=:Param2", 
					Employee.class);
		else if ((("Prenume".equals(f1)) && ("Departament".equals(f2))) || (("Departament".equals(f1)) && ("Prenume".equals(f2))))
			query = em.createQuery("SELECT e FROM Employee e WHERE e.firstname=:Param1 AND e.department=:Param2", 
					Employee.class);
		else if ((("Prenume".equals(f1)) && ("Team Leader".equals(f2))) || (("Team Leader".equals(f1)) && ("Prenume".equals(f2))))
			query = em.createQuery("SELECT e FROM Employee e WHERE e.firstname=:Param1 AND e.teamleaderro=:Param2", 
					Employee.class);
		else if ((("Prenume".equals(f1)) && ("Tipul angajarii".equals(f2))) || (("Tipul angajarii".equals(f1)) && ("Prenume".equals(f2))))
			query = em.createQuery("SELECT e FROM Employee e WHERE e.firstname=:Param1 AND e.empl_type=:Param2", 
					Employee.class);
		else if ((("Prenume".equals(f1)) && ("Pozitie".equals(f2))) || (("Pozitie".equals(f1)) && ("Prenume".equals(f2))))
			query = em.createQuery("SELECT e FROM Employee e WHERE e.firstname=:Param1 AND e.position=:Param2", 
					Employee.class);
		else if ((("Team Leader".equals(f1)) && ("Departament".equals(f2))) || (("Departament".equals(f1)) && ("Team Leader".equals(f2))))
			query = em.createQuery("SELECT e FROM Employee e WHERE e.teamleaderro=:Param1 AND e.department=:Param2", 
					Employee.class);
		else if ((("Tipul angajarii".equals(f1)) && ("Departament".equals(f2))) || (("Departament".equals(f1)) && ("Tipul angajarii".equals(f2))))
			query = em.createQuery("SELECT e FROM Employee e WHERE e.empl_type=:Param1 AND e.department=:Param2", 
					Employee.class);
		else if ((("Pozitie".equals(f1)) && ("Departament".equals(f2))) || (("Departament".equals(f1)) && ("Pozitie".equals(f2))))
			query = em.createQuery("SELECT e FROM Employee e WHERE e.position=:Param1 AND e.department=:Param2", 
					Employee.class);
		else if ((("Team Leader".equals(f1)) && ("Tipul angajarii".equals(f2))) || (("Tipul angajarii".equals(f1)) && ("Team Leader".equals(f2))))
			query = em.createQuery("SELECT e FROM Employee e WHERE e.teamleaderro=:Param1 AND e.empl_type=:Param2", 
					Employee.class);
		else if ((("Team Leader".equals(f1)) && ("Pozitie".equals(f2))) || (("Pozitie".equals(f1)) && ("Team Leader".equals(f2))))
			query = em.createQuery("SELECT e FROM Employee e WHERE e.teamleaderro=:Param1 AND e.position=:Param2", 
					Employee.class);
		else if ((("Tipul angajarii".equals(f1)) && ("Pozitie".equals(f2))) || (("Pozitie".equals(f1)) && ("Tipul angajarii".equals(f2))))
			query = em.createQuery("SELECT e FROM Employee e WHERE e.empl_type=:Param1 AND e.position=:Param2", 
					Employee.class);
		query.setParameter("Param1", v1);
		query.setParameter("Param2", v2);
		List<Employee> employee = query.getResultList();
		for (int i = 0; i < employee.size(); i++){
			EmployeeDTO angajat = new EmployeeDTO();
			angajat = conv.convertEmployeeToDTO(employee.get(i));
			angajati.add(angajat);
		}
		
		return angajati;
	}

	@Override
	public List<EmployeeDTO> getAngajatiByOne(String f1, String v1) {
		List<EmployeeDTO> angajati = new ArrayList<EmployeeDTO>();
		if ("Nume".equals(f1)){
			TypedQuery<Employee> query = em
					.createQuery(
							"SELECT e FROM Employee e WHERE e.lastname = :Param ",
							Employee.class);
			query.setParameter("Param", v1);
			List<Employee> employee = query.getResultList();
			try {
				for (int i = 0; i < employee.size(); i++){
					EmployeeDTO angajat = new EmployeeDTO();
					angajat = conv.convertEmployeeToDTO(employee.get(i));
					angajati.add(angajat);
				}
			}
			catch (Exception e){
				e.printStackTrace();
			}	
		}
		else if ("Prenume".equals(f1)){
			// ezt is!!!!
			TypedQuery<Employee> query = em
					.createQuery(
							"SELECT e FROM Employee e WHERE e.firstname = :Param ",
							Employee.class);
			query.setParameter("Param", v1);
			List<Employee> employee = query.getResultList();
			try {
				for (int i = 0; i < employee.size(); i++){
					EmployeeDTO angajat = new EmployeeDTO();
					angajat = conv.convertEmployeeToDTO(employee.get(i));
					angajati.add(angajat);
				}
			}
			catch (Exception e){
				e.printStackTrace();
			}
		}
		else if ("Departament".equals(f1)){
			// ezt is
			TypedQuery<Employee> query = em
					.createQuery(
							"SELECT e FROM Employee e WHERE e.department = :Param ",
							Employee.class);
			query.setParameter("Param", v1);
			List<Employee> employee = query.getResultList();
			try {
				for (int i = 0; i < employee.size(); i++){
					EmployeeDTO angajat = new EmployeeDTO();
					angajat = conv.convertEmployeeToDTO(employee.get(i));
					angajati.add(angajat);
				}
			}
			catch (Exception e){
				e.printStackTrace();
			}
		}
		else if ("Team Leader".equals(f1)){
			// eztr is 
			TypedQuery<Employee> query = em
					.createQuery(
							"SELECT e FROM Employee e WHERE e.teamleaderro = :Param ",
							Employee.class);
			query.setParameter("Param", v1);
			List<Employee> employee = query.getResultList();
			try {
				for (int i = 0; i < employee.size(); i++){
					EmployeeDTO angajat = new EmployeeDTO();
					angajat = conv.convertEmployeeToDTO(employee.get(i));
					angajati.add(angajat);
				}
			}
			catch (Exception e){
				e.printStackTrace();
			}
		}
		else if ("Tipul angajarii".equals(f1)){
			//ezt is 
			TypedQuery<Employee> query = em
					.createQuery(
							"SELECT e FROM Employee e WHERE e.empl_type = :Param ",
							Employee.class);
			query.setParameter("Param", v1);
			List<Employee> employee = query.getResultList();
			try {
				for (int i = 0; i < employee.size(); i++){
					EmployeeDTO angajat = new EmployeeDTO();
					angajat = conv.convertEmployeeToDTO(employee.get(i));
					angajati.add(angajat);
				}
			}
			catch (Exception e){
				e.printStackTrace();
			}
		}
		else if ("Pozitie".equals(f1)){
			// ezt is 
			TypedQuery<Employee> query = em
					.createQuery(
							"SELECT e FROM Employee e WHERE e.position = :Param ",
							Employee.class);
			query.setParameter("Param", v1);
			List<Employee> employee = query.getResultList();
			try {
				for (int i = 0; i < employee.size(); i++){
					EmployeeDTO angajat = new EmployeeDTO();
					angajat = conv.convertEmployeeToDTO(employee.get(i));
					angajati.add(angajat);
				}
			}
			catch (Exception e){
				e.printStackTrace();
			}
		}
		return angajati;
		
	}
	
	


	
}
